package com.tengfei.blogsystem.service.impl;

import com.tengfei.blogsystem.dao.LooperDao;
import com.tengfei.blogsystem.pojo.Looper;
import com.tengfei.blogsystem.response.ResponseResult;
import com.tengfei.blogsystem.response.ResponseState;
import com.tengfei.blogsystem.service.ILopperService;
import com.tengfei.blogsystem.utils.Constants;
import com.tengfei.blogsystem.utils.SnowFlakeIdWorker;
import com.tengfei.blogsystem.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

/**
 * @version v1.0
 * @author} feige
 * @date 2021-10-28-15:44
 * @description
 */
@Service
@Transactional
public class LopperServiceImpl implements ILopperService {

    @Autowired
    private SnowFlakeIdWorker idWorker;
    @Autowired
    private LooperDao looperDao;

    @Override
    public ResponseResult listLooper(int page, int size) {
        // 分页查询
        if (page < Constants.PageValue.DEFAULT_PAGE) {
            page = Constants.PageValue.DEFAULT_PAGE;
        }
        if (size < Constants.PageValue.MIN_SIZE) {
            size = Constants.PageValue.MIN_SIZE;
        }
        // 排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        Page<Looper> categories = looperDao.findAll(pageable);
        return new ResponseResult(ResponseState.SUCCESS, "查询轮播图列表成功", categories);
    }

    @Override
    public ResponseResult addLooper(Looper looper) {
        // 检查数据
        String title = looper.getTitle();
        if (TextUtils.isEmpty(title)) {
            return new ResponseResult(ResponseState.ERROR, "轮播图标题不能为空");
        }
        String imageUrl = looper.getImageUrl();
        if (TextUtils.isEmpty(imageUrl)) {
            return new ResponseResult(ResponseState.ERROR, "轮播图图片不能为空");
        }
        String targetUrl = looper.getTargetUrl();
        if (TextUtils.isEmpty(targetUrl)) {
            return new ResponseResult(ResponseState.ERROR, "轮播图跳转链接不能为空");
        }
        // 补充数据
        looper.setId(String.valueOf(idWorker.nextId()));
        looper.setState("1");
        looper.setCreateTime(new Date());
        looper.setUpdateTime(new Date());
        // 保存数据
        looperDao.save(looper);
        return new ResponseResult(ResponseState.SUCCESS, "添加成功");
    }

    @Override
    public ResponseResult getLooper(String looperId) {
        Looper looperById = looperDao.findOneById(looperId);
        if (looperById != null) {
            return new ResponseResult(ResponseState.SUCCESS, "查找轮播图成功", looperById);
        }
        return new ResponseResult(ResponseState.ERROR, "轮播图不存在");
    }

    @Override
    public ResponseResult deleteLooper(String looperId) {
        looperDao.deleteById(looperId);
        return new ResponseResult(ResponseState.SUCCESS, "删除轮播图成功");
    }

    @Override
    public ResponseResult updateLooper(String looperId, Looper looper) {
        Looper looperById = looperDao.findOneById(looperId);
        if (looperById == null) {
            return new ResponseResult(ResponseState.ERROR, "轮播图不存在");
        }
        // 检查数据
        String title = looper.getTitle();
        if (!TextUtils.isEmpty(title)) {
            looperById.setTitle(title);
        }
        String imageUrl = looper.getImageUrl();
        if (!TextUtils.isEmpty(imageUrl)) {
            looperById.setImageUrl(imageUrl);
        }
        String targetUrl = looper.getTargetUrl();
        if (!TextUtils.isEmpty(targetUrl)) {
            looperById.setTargetUrl(targetUrl);
        }
        String state = looper.getState();
        if (!TextUtils.isEmpty(state)) {
            looperById.setState(state);
        }
        looperById.setUpdateTime(new Date());
        return new ResponseResult(ResponseState.SUCCESS, "更新轮播图成功");
    }
}
